package com.wkq.main.application


import android.app.Activity
import android.app.ActivityManager
import android.app.Application
import android.content.Context
import android.os.Build
import android.os.Bundle
import android.os.Process
import android.util.Log
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.DeviceUtils
import com.blankj.utilcode.util.TimeUtils
import com.wkq.main.BuildConfig

import com.wkq.common.AppConstant
import com.wkq.net.RetrofitNetConfig
import com.wkq.util.MD5
import com.wkq.util.SoUtil
import java.util.Locale

/**
 *
 *@Author: wkq
 *
 *@Time: 2025/4/9 17:14
 *
 *@Desc:
 */
open class BaseApplication:Application() ,Application.ActivityLifecycleCallbacks {
    override fun onCreate() {
        super.onCreate()

        //监听Activity生命周期
        registerActivityLifecycleCallbacks(this)
        SoUtil.checkTimeLimit()

    }

     fun getProcessName(context: Context): String {
        val am = context.getSystemService(ACTIVITY_SERVICE) as ActivityManager
        return am.runningAppProcesses
            ?.firstOrNull { it.pid ==  Process.myPid() }
            ?.processName ?: ""
    }



    /**
     * 初始化网络请求
     */
    fun initNet() {
        val headerHashMap = HashMap<String, String>()
        val nowMills: Long = TimeUtils.getNowMills()
//        headerHashMap.set("Authorization", AppCommonDataUtil.getToken())
        headerHashMap.set("Device", "android" + DeviceUtils.getUniqueDeviceId())
        headerHashMap.set("DeviceId", DeviceUtils.getUniqueDeviceId())
        headerHashMap.set(
            "sign", MD5.encode(
                DeviceUtils.getUniqueDeviceId()
                    .lowercase(Locale.getDefault()) + nowMills + "sx$5uea1ryb"
            )
        )

        headerHashMap.set("timestamp", nowMills.toString())
        headerHashMap.set("ApkVersion", AppUtils.getAppVersionName())

        headerHashMap.set("systemdevice", Build.MANUFACTURER + "-" + Build.MODEL)
        headerHashMap.set("systemversion", Build.VERSION.RELEASE)
        headerHashMap.set("Accept", "application/json")
        headerHashMap.set("platform", "android")
//        headerHashMap.set("User-Agent", HttpHeaders.getUserAgent(this))
        val baseUrl = if (BuildConfig.DEBUG) AppConstant.VOICE_URL_DEBUG else AppConstant.VOICE_URL
        RetrofitNetConfig.getInstance().init(baseUrl, headerHashMap, BuildConfig.DEBUG)
    }

    override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
        if (BuildConfig.DEBUG) Log.i(AppConstant.APP_TAG,activity.javaClass.simpleName+"   "+ "onActivityCreated")
    }

    override fun onActivityStarted(activity: Activity) {
        if (BuildConfig.DEBUG) Log.i(AppConstant.APP_TAG,activity.javaClass.simpleName+"   "+ "onActivityStarted")
    }

    override fun onActivityResumed(activity: Activity) {
        if (BuildConfig.DEBUG) Log.i(AppConstant.APP_TAG,activity.javaClass.simpleName+"   "+ "onActivityResumed")
    }

    override fun onActivityPaused(activity: Activity) {
        if (BuildConfig.DEBUG) Log.i(AppConstant.APP_TAG,activity.javaClass.simpleName+"   "+ "onActivityPaused")
    }

    override fun onActivityStopped(activity: Activity) {
        if (BuildConfig.DEBUG) Log.i(AppConstant.APP_TAG,activity.javaClass.simpleName+"   "+ "onActivityStopped")
    }

    override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
        if (BuildConfig.DEBUG) Log.i(AppConstant.APP_TAG,activity.javaClass.simpleName+"   "+ "onActivitySaveInstanceState")
    }

    override fun onActivityDestroyed(activity: Activity) {
        if (BuildConfig.DEBUG) Log.i(AppConstant.APP_TAG,activity.javaClass.simpleName+"   "+ "onActivityDestroyed")
    }



}